function DetectAreasPostProcess(fin, fout, bGenderFile)

load(fin);
load(selecttypesfilefull);

sz = size(areascores);

len = sz(1);
nobjs = sz(2);
ntypes = sz(3);

nFemaleSpecificIndex = find(bFemales == 1 & bAssignGenders == 0);
nMaleSpecificIndex = find(bFemales == 0 & bAssignGenders == 0);


%Use male/female identification to post process areas that are not used to 
%identify gender but are gender specific - for example, in one pair a male 
%may have a more redish beak than the female (but difference not reliable
%enough to use for gender identification). so signal warning if for 
%example female beak type is identified on male. 
if bGenderFile == 1 & (~isempty(nFemaleSpecificIndex) | ~isempty(nMaleSpecificIndex))
    
    bSpecifics = zeros(ntypes, 1);
    
    if ~isempty(nFemaleSpecificIndex)
        bSpecifics(nFemaleSpecificIndex) = 1;
    end
    
    if ~isempty(nMaleSpecificIndex)
        bSpecifics(nMaleSpecificIndex) = 1;
    end


    for i = 1:len
        if objs(i) == nfindobjs
            
            for j = 1:nfindobjs
                for k = 1:ntypes
                  
                    if bSpecifics(k) == 1 & areasx1(i, j, k) > 0  & bgenderwarnings(i) == 0 %& bwarnings(i, j, k) == 0
                        
                        %Area type detected is female type (e.g. female beak) but gender type
                        %is male so set warning for detection of area (e.g.
                        %beak)
                        if bFemales(k) == 1 & gendertypes(i, j) == 1 
                            bwarnings(i, j, k) = 2;
                        elseif bFemales(k) == 0 & gendertypes(i, j) == 0 %Area type detected is male type but gender is female so set warning
                            bwarnings(i, j, k) = 2;    
                        end
                        
                    end
                                    
                end
            end
                            
        end
    end
    
end


distconst = 2.0;
maxframes = 15;

pmin = 0.8;
nprevwindow = 5;
maxmissing = nprevwindow*(1-pmin);
nstart = nprevwindow+1;

removeareas= zeros(len, nfindobjs, ntypes);

for i = nstart:len-1
    if objs(i) == nfindobjs
            
        i0 = i-nprevwindow;
        i1 = i;
        
        for j = 1:nfindobjs
            for k = 1:ntypes
                
                %For previous period of maxframes, check that areas 
                %detected of current type did not have any warnings
                %and were detected in each frame. 
                a = find(bwarnings(i0:i1, j, k) ~= 0);
                b = find(areasx0(i0:i1, j, k) == 0);
                                                
                if isempty(a) & length(b) <= maxmissing
                                 
                    xs = areasx2(i0:i1, j, k)-areasx1(i0:i1, j, k);
                    ys = areasy2(i0:i1, j, k)-areasy1(i0:i1, j, k);
                    
                    ls = sqrt(xs.*xs+ys.*ys);                                        
                    
                    avglens = mean(ls);
                    maxdist = avglens*distconst;
                
                    for l = i0:i1-1
                        dx = areasx0(l, j, k) - areasx0(l+1, j, k);
                        dy = areasy0(l, j, k) - areasy0(l+1, j, k);
                        
                        d = sqrt(dx*dx+dy*dy);
                        
                        if d > maxdist
							removeareas(l, j, k) = 1;  
							bwarnings(l, j, k) = 1;
                        end
                    end                
                end
                
            end
        end
        
    end
end


for j = 1:nfindobjs
    for k = 1:ntypes
        a = find(removeareas(a, j, k) == 1);
        bwarnings(a, j, k) = 1;
    end
end


if bGenderFile == 0
    bgenderwarnings = zeros(len, 1);
    gendertypes = -ones(len, nfindobjs);
end



save(fout, 'objs', 'x1s', 'x2s', 'y1s', 'y2s', 'ex0s', 'ey0s', 'ephis', 'eas', 'ebs', 'ets', ...
    'xheights', 'yheights', 'areas', 'nprocessstart', 'imgx', 'imgy', 'imghalfx', 'imghalfy', ...
    'areasx1', 'areasx2', 'areasy1', 'areasy2', 'areasx0', 'areasy0', 'areascores',...
    'nfindobjs', 'nfindmaxobjs', 'nstartframe', 'nendframe', ...
    'cdistmeans', 'cdiststds', 'scoremeans', 'scorestds', 'bestscores', 'bestscoreindexes',...
    'bwarnings', 'detecttypes', 'selecttypesfilefull', 'gendertypes', 'bgenderwarnings', 'objs1',...
    'bsegmented','dminscores', 'dmincdists');





